package com.primaryedu.ui.app.box;

import com.primaryedu.R;
import com.primaryedu.bean.MastermailBean;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * 校长信箱适配器
 *
 * @author jun.wang
 * @description
 * @data 2018年1月5日
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class BoxListAdapter extends BaseAdapter {

    private Context context;
    LayoutInflater inflater;
    private List<MastermailBean.DataBean.ListBean> list = new ArrayList<>();

    public BoxListAdapter(Context context, List<MastermailBean.DataBean.ListBean> list) {
        this.context = context;
        this.list = list;
        inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void updateMasterView(List<MastermailBean.DataBean.ListBean> nowList) {
        this.list = nowList;
        this.notifyDataSetChanged();//强制动态刷新数据进而调用getView方法
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (null == convertView) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_box_list, null);
            viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
            viewHolder.tv_masterbox_name_time = (TextView) convertView.findViewById(R.id.tv_masterbox_name_time);
            viewHolder.tv_masterbox_content = (TextView) convertView.findViewById(R.id.tv_masterbox_content);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        MastermailBean.DataBean.ListBean mb = list.get(position);
        viewHolder.tv_masterbox_name_time.setText(mb.getUserName() + "  " + mb.getCreateTime());
        viewHolder.tv_masterbox_content.setText(mb.getContent());
        if (mb.getStatus() == 0) {
            viewHolder.image.setBackground(context.getResources().getDrawable(R.drawable.message_no_read));
        } else if (mb.getStatus() == 1) {
            viewHolder.image.setBackground(context.getResources().getDrawable(R.drawable.message_read));
        }


        return convertView;
    }

    class ViewHolder {
        ImageView image;
        TextView tv_masterbox_content;
        TextView tv_masterbox_name_time;
    }


}
